<?php
/**
 * 新闻资讯
 * User: E 房cms www.fangcms.com
 * Date: 2015/1/20
 * Time: 10:51
 */
namespace Home\Controller;
use Org\Yike\FangCms;
use Org\Util\Page;
class NewsController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        //点评排行
        $this->news_hits = M('article')->where(array('status'=>1))->limit(10)->order('hits desc')->select();
        //推荐楼盘
        $this->house_list = M('house')->where(array('status'=>1,'is_rec'=>1))->order('ordid desc')->limit(10)->select();

    }

    public function index(){
        $FangCms = new FangCms();
        $key = trim($_GET['key']);
        if($key){
            $this->page_arr = $FangCms->getNewsList(10,array('a.status'=>1,'a.title'=>array('like','%'.$key.'%')),true);
            $this->display('so');
        }else{
            $this->page_arr = $FangCms->getNewsList(12,array('a.status'=>1),true);
            $news_cate = M('article_cate')->where(array('status'=>1,'pid'=>0))->order('ordid desc')->limit(10)->select();
            $this->news_cate = $news_cate;
            $this->ad_list = getAds(2,8);
            $this->alias = $news_cate[0]['alias'];
            $this->display();
        }
    }

    public function lists(){
        $FangCms = new FangCms();
        $alias = I('get.alias');
        if(!$alias){
            $this->error('找不到对应的分类名称');
        }
        $news_cate = M('article_cate')->where(array('status'=>1,'pid'=>0))->order('ordid desc')->limit(10)->select();

        $cate = M('article_cate')->where(array('status'=>1,'alias'=>$alias))->find();

        $this->cate_nav = $FangCms->currentLocation('article_cate',array('status'=>1,'id'=>$cate['id']));

        $cate_ids = $FangCms->getCate('article_cate',4,array('pid'=>$cate['id']));
        array_push($cate_ids,$cate['id']);

        //按条件查询
        $where = array('a.status'=>1,'a.cate_id'=>array('in',implode(',',$cate_ids)));
        $this->page_arr = $FangCms->getNewsList(18,$where,true);
        $this->alias = $alias;
        $this->news_cate = $news_cate;
        $this->cate = $cate;
        $this->seoKey($cate,'name');
        $this->display();
    }

    //问答详细
    public function detail(){
        $FangCms = new FangCms();
        $id = intval($_GET['id']);
        if(!$id){
            $this->error('没有找到对应ID信息');
        }
        $info = M('article')->where(array('id'=>$id,'status'=>1))->find();
        if(!$info['id']){
            $this->error('没有找到对应ID信息');
        }
        $FangCms->setInc($id,'article');
        //相关楼盘
        if($info['house_id']){
            $this->house = M('house')->where(array('id'=>$info['house_id'],'status'=>1))->find();
            $this->address = explode('|',$this->house['address']);
            $this->tel = explode('|',$this->house['tel']);
        }

        //当前位置
        $this->cate_nav = $FangCms->currentLocation('article_cate',array('status'=>1,'id'=>$info['cate_id']));

        $this->cate = M('article_cate')->where(array('id'=>$info['cate_id']))->find();

        //相关资讯
        $this->rec_list = M('article')->where(array('status'=>1,'cate_id'=>$info['cate_id'],'id'=>array('gt',$info['id'])))->order('id asc')->limit(6)->select();

        //所属会员
        if($info['user_id']){
            $this->user_news = getUserInfo($info['user_id'],'');
            //求和文章数
            $this->news_count = M('article')->where(array('status'=>1,'user_id'=>$info['user_id']))->count();
            //求全部文章点评数
            $this->news_hits_count = M('article')->where(array('user_id'=>$info['user_id']))->sum('hits');
            //会员发布的文章
            $this->user_news_list = M('article')->where(array('status'=>1,'id'=>array('neq',$info['id']),'user_id'=>$info['user_id']))->order('id asc')->limit(4)->select();
        }

        $this->info = $info;
        $this->seoKey($info,'title');
        $this->display();
    }

    public function so(){
        $key = urldecode(trim($_GET['key']));
        if($key==''){
            $this->error('找不到对应的搜索词信息');
        }
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',2);
        $this->page_arr = $FangCms->getAskList(25,array('a.status'=>1,'a.title'=>array('like','%'.$key.'%')),true);
        $this->solve_status = solveStatus();
        $this->v=$key;
        $this->display();
    }



}